package ee.sepp.aui.ui.views;

import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import ee.sepp.aui.ui.MyVaadinUI;
import ee.sepp.aui.ui.content.mngr.AuiContnentManager;
import ee.sepp.aui.ui.menu.AuiMenu;
import ee.sepp.aui.ui.views.footer.AuiFooter;
import ee.sepp.aui.ui.views.header.AuiHeader;

/** Main view with a menu */
@SuppressWarnings("serial")
public class MainView extends VerticalLayout implements View {

	Panel header;
	private AuiContnentManager cntntMng;

	// Menu navigation button listener
	class ButtonListener implements Button.ClickListener {
		String menuitem;

		public ButtonListener(String menuitem) {
			this.menuitem = menuitem;
		}

		@Override
		public void buttonClick(ClickEvent event) {
			// Navigate to a specific state
			getUI().getNavigator().navigateTo(MyVaadinUI.MAINVIEW + "/" + menuitem);
		}
	}

	public MainView() {
		setSizeFull();

		// Layout LEFT RIGHT
		HorizontalLayout mainHorizLayout = new HorizontalLayout();
		mainHorizLayout.setSizeFull();

		// LEFT - menu
		AuiMenu menu = new AuiMenu();
		
		menu.setHeight("100%");
		menu.setWidth(null);
		VerticalLayout menuContent = new VerticalLayout();
		menuContent.addComponent(new Button("Pig", new ButtonListener("pig")));
		menuContent.addComponent(new Button("Cat", new ButtonListener("cat")));
		menuContent.addComponent(new Button("Dog", new ButtonListener("dog")));
		menuContent.addComponent(new Button("Reindeer", new ButtonListener("reindeer")));
		menuContent.addComponent(new Button("Penguin", new ButtonListener("penguin")));
		menuContent.addComponent(new Button("Sheep", new ButtonListener("sheep")));
		menuContent.setWidth(null);
		menuContent.setMargin(true);
		menu.setContent(menuContent);
		
		mainHorizLayout.addComponent(menu);
		
		// RIGHT - header, content, footer
		VerticalLayout rightVertLayout = new VerticalLayout();
		mainHorizLayout.addComponent(rightVertLayout);
		rightVertLayout.setSizeFull();
		mainHorizLayout.setExpandRatio(rightVertLayout, 1.0f);
		
		header = new AuiHeader();
		cntntMng = new AuiContnentManager();

		rightVertLayout.addComponent(header);
		rightVertLayout.addComponent(cntntMng.getContent());
		rightVertLayout.addComponent(new AuiFooter());
		rightVertLayout.setExpandRatio(cntntMng.getContent(), 1.0f);

		
		addComponent(mainHorizLayout);
		setExpandRatio(mainHorizLayout, 1.0f);
	}

	@Override
	public void enter(ViewChangeEvent event) {
		VerticalLayout panelContent = new VerticalLayout();
		panelContent.setSizeFull();
		panelContent.setMargin(true);
		cntntMng.getContent().setContent(panelContent); // Also clears

		if (event.getParameters() == null || event.getParameters().isEmpty()) {
			panelContent.addComponent(new Label("Nothing to see here, " + "just pass along."));
			return;
		}

		// Display the fragment parameters
		Label watching = new Label("You are currently watching a " + event.getParameters());
		watching.setSizeUndefined();
		panelContent.addComponent(watching);
		panelContent.setComponentAlignment(watching, Alignment.MIDDLE_CENTER);

		// Some other content
		Embedded pic = new Embedded(null, new ThemeResource("img/" + event.getParameters() + "-128px.png"));
		panelContent.addComponent(pic);
		panelContent.setExpandRatio(pic, 1.0f);
		panelContent.setComponentAlignment(pic, Alignment.MIDDLE_CENTER);

		Label back = new Label("And the " + event.getParameters() + " is watching you");
		back.setSizeUndefined();
		panelContent.addComponent(back);
		panelContent.setComponentAlignment(back, Alignment.MIDDLE_CENTER);
	}
}
